package __mysql

import (
	"database/sql"
	"fmt"
	"log"
	"math/rand"
)
import _ "github.com/go-sql-driver/mysql"

func Database() {
	db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:33306)/test1")
	checkErr(err)
	defer func(db *sql.DB) {
		err := db.Close()
		checkErr(err)
	}(db)

	// 插入数据
	type Order struct {
		createTime  int
		uid         int
		amount      int
		productId   int
		productName string
	}
	orders := make([]Order, 0)
	for i := 0; i < 90000; i++ {
		createTime := 1646054388 + int(rand.Float64()*500)
		uid := int(rand.Float64() * 5000)
		amount := int(rand.Float64() * 5000)
		productId := int(rand.Float64() * 5000)
		productName := fmt.Sprintf("钻石x%d", int(rand.Float64()*1000))
		ord := Order{createTime, uid, amount, productId, productName}
		orders = append(orders, ord)
	}
	query := "insert into `order` (create_time, uid, amount, product_id, product_name) values (?, ?, ?, ?, ?)"
	stmt, err := db.Prepare(query)
	checkErr(err)
	defer stmt.Close()
	for _, order := range orders {
		_, err := stmt.Exec(order.createTime, order.uid, order.amount, order.productId, order.productName)
		checkErr(err)
	}
}

func checkErr(err error) {
	if err != nil {
		log.Println(err)
	}
}
